<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <el-button type="primary" @click="add()">添加</el-button>

    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="intro" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="estimatedTime" label="预计时间(分钟)" align="center">
      </el-table-column>
      <el-table-column prop="costGold" label="金币花费" align="center">
      </el-table-column>
      <el-table-column prop="playNum" label="预计播放量" align="center">
      </el-table-column>
      <el-table-column prop="isCustom" label="是否自定义" align="center">
        <template slot-scope="{ row, $index }">
          <div>{{ row.isCustom == "0" ? '否' : '是' }}</div>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <el-tooltip content="修改套餐" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"
              :disabled="!list.isCustom ? false : true"></el-button>
          </el-tooltip>
          <el-popconfirm title="是否删除套餐？" icon="el-icon-info" icon-color="red" @confirm="getDelete(row.id)">
            <el-tooltip content="删除套餐" placement="top" slot="reference">
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框  -->
    <el-dialog title="修改套餐推广" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom"  ref="ruleForm">
        <el-form-item label="名称" label-width="100px" prop="intro">
          <el-input autocomplete="off" v-model="tmFrom.intro"></el-input>
        </el-form-item>
        <el-form-item label="金币花费" label-width="100px" prop="costGold">
          <el-input autocomplete="off" v-model="tmFrom.costGold"></el-input>
        </el-form-item>
        <el-form-item label="预计播放量" label-width="100px" prop="playNum">
          <el-input autocomplete="off" v-model="tmFrom.playNum"></el-input>
        </el-form-item>
        <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="tmFrom.estimatedTime"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="UpdateBillingRules">修改</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框  -->
    <el-dialog title="添加套餐推广" :visible.sync="dialogFormVisible1">
      <el-form style="width:80%" :model="list1" :rules="rules1" ref="ruleForm">
        <el-form-item label="名称" label-width="120px" prop="intro">
          <el-input autocomplete="off" v-model="list1.intro"></el-input>
        </el-form-item>
        <el-form-item label="金币花费" label-width="120px" prop="costGold">
          <el-input autocomplete="off" v-model="list1.costGold"></el-input>
        </el-form-item>
        <el-form-item label="预计播放量" label-width="120px" prop="playNum">
          <el-input autocomplete="off" v-model="list1.playNum"></el-input>
        </el-form-item>
        <el-form-item label="预计时间/分钟" label-width="120px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="list1.estimatedTime"></el-input>
        </el-form-item>
        <el-form-item label="是否自定义" label-width="120px">
          <el-select v-model="list1.isCustom" placeholder="请选择是否自定义"  prop="isCustom">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="getAddDynamicExtensionConfiguration">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Dynamic',
  data() {

    return {
      // 修改时的数据
      //列表数据
      list: [],
      // 添加列表
      list1: {
        // 金币花费
        costGold: "",
        // 预计时间
        estimatedTime: "",
        // 名称
        intro: "",
        // 是否自定义
        isCustom: "",
        // 预计播放量
        playNum: ""
      },

      // 对话框显示与隐藏控制
      dialogFormVisible: false,
      dialogFormVisible1: false,
      // 表单对象
      tmFrom: {},
      // 当前为修改还是添加 修改为false 添加为true
      state: true,
      // 表单验证

      rules1: {
        intro: [
          { required: true, message: '请输入活动名称', trigger: 'chang' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        costGold: [
          { required: true, message: '请输入金币花费', trigger: 'int' },
          // { min: 3, max: 5, message: '请输入金币花费', trigger: 'blur' }
        ],
        playNum: [
          { required: true, message: '请输入预计播放量', trigger: 'int' },
          // { min: 1, max: 99999, message: '请输入预计播放量', trigger: 'int' }
        ],
        estimatedTime: [
          { required: true, message: '请输入预计时间', trigger: 'change' },
          // { min: 1, max: 9999, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {
    this.getDynamicExtensionConfigurationList()
  },

  methods: {
    // 获取推广套餐列表
    async getDynamicExtensionConfigurationList() {
      let res = await this.$API.system.getDynamicExtensionConfigurationList()
      console.log(res, "108")
      this.list = res.data;
    },
    // 刷新
    getDynamicExtensionProcessList() {
      this.getDynamicExtensionConfigurationList()
    },
    // 添加一条数据窗口弹出
    add() {
      // this.state = true
      this.dialogFormVisible1 = true
      this.list1 = {
        // 金币花费
        costGold: "",
        // 预计时间
        estimatedTime: "",
        // 名称
        intro: "",
        // 是否自定义
        isCustom: "",
        // 预计播放量
        playNum: ""
      }
    },
    // 添加一条数据发送到后端
    getAddDynamicExtensionConfiguration() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.system.getAddDynamicExtensionConfiguration(this.list1);
          console.log(res);
          this.dialogFormVisible1 = false;
          this.getDynamicExtensionConfigurationList()
        } else {
          console.log('error submit!!');
          return false;
        }
      })

      // console.log(this.tmFrom);
    },
    // 点击修改
    ubpateBannar(row) {
      this.state = false
      this.dialogFormVisible = true;
      // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
      this.tmFrom = { ...row };
      console.log(this.tmFrom);
    },
    // 点击修改
    UpdateBillingRules() {
      console.log(this.id);
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.system.getUpdateDynamicExtensionConfiguration(this.tmFrom);
          this.dialogFormVisible = false;
          this.getDynamicExtensionConfigurationList()
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除
    async getDelete(id) {
      let res = await this.$API.system.getDeleteDynamicExtensionConfiguration({ id: id });
      console.log(res);
      this.getDynamicExtensionConfigurationList()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
